package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetLastMathGameFootStepBean;
import com.rjwh.dingdong.client.bean.localbean.GetLastMathGameFootStepBean;
import com.rjwh.dingdong.client.bean.localbean.MathGameFootStepListData;
import com.rjwh.dingdong.client.bean.localbean.MathGameFootStepTotal;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.MathGameMyTracksModel;
import com.rjwh.dingdong.client.mvp.presenter.IMathGameMyTracksPresenter;
import com.rjwh.dingdong.client.mvp.view.MathGameMyTracksView;
import com.rjwh.dingdong.client.service.PlaybackService;

import java.util.List;

/**
 * @author Kay
 * @ClassName MathGameMyTracksPresenter
 * @Description
 * @date 2016-9-5
 * @history 2016-9-5 author: description:
 */
public class MathGameMyTracksPresenter extends BasePresenter<MathGameMyTracksView> implements IMathGameMyTracksPresenter {

    private MathGameMyTracksModel model;
    private PlaybackService mPlaybackService;
    private boolean mIsServiceBound;

    public MathGameMyTracksPresenter(Context context, MathGameMyTracksView mMvpView) {
        super(context, mMvpView);
        model = new MathGameMyTracksModel(this);
    }

    /**
     * 取游戏记录
     *
     * @param bookid 游戏id
     */
    @Override
    public void doGeLastMathGameFootStep(String bookid,String packageversion) {
        if (isViewAttached()) {
            getMvpView().hideErrorView();
            getMvpView().showLoadingView();
            model.doGeLastMathGameFootStep(bookid, packageversion);
        }
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().hideLoadingView();
            switch (businessType) {
                case NetConstant.GETLASTMATHGAMEFOOTSTEPBYVERSION:
                    ResGetLastMathGameFootStepBean resGetLastMathGameFootStepBean = (ResGetLastMathGameFootStepBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetLastMathGameFootStepBean != null && resGetLastMathGameFootStepBean.getData() != null) {
                        getMvpView().hideErrorView();
                        GetLastMathGameFootStepBean data = resGetLastMathGameFootStepBean.getData();
                        MathGameFootStepTotal total = data.getTotal();
                        List<MathGameFootStepListData> gamelist = data.getGamelist();
                        if (total != null && gamelist != null) {
                            getMvpView().loadData(total, gamelist);
                        } else {
                            getMvpView().showErrorView();
                        }
                    } else {
                        getMvpView().showErrorView();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mPlaybackService = ((PlaybackService.LocalBinder) service).getService();
            getMvpView().onPlaybackServiceBound(mPlaybackService);
        }

        public void onServiceDisconnected(ComponentName className) {
            mPlaybackService = null;
            getMvpView().onPlaybackServiceUnbound();
        }
    };

    private void bindPlaybackService() {
        getContext().bindService(new Intent(getContext(), PlaybackService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsServiceBound = true;
    }

    private void unbindPlaybackService() {
        if (mIsServiceBound) {
            // Detach our existing connection.
            getContext().unbindService(mConnection);
            mIsServiceBound = false;
        }
    }

    public void subscribe() {
        bindPlaybackService();
    }

    @Override
    public void detachView() {
        unbindPlaybackService();
        model.detachView();
        detachViews();
    }

}
